package com.myebills.ebpsfrontend.cust.controller;

import com.checkfree.isolutions.persist.PersistServer;
import com.checkfree.isolutions.property.Properties;
import com.checkfree.isolutions.property.PropertiesConfig;
import com.checkfree.isolutions.utility.*;
import java.sql.*;


public class EbppxCorpCustomer{

	private String userName = "";	private String password = "";
	private String emailAdd = "";
	private String accounts = "";

	public EbppxCorpCustomer(){
	}
    private static Connection getDbConnection() throws Exception {        Connection conn=null;    	String jdbcDriver = "";   		String jdbcDataSource = "";   		String username = "";   		String password = "";
        try{			com.checkfree.isolutions.property.Properties appCfg = com.checkfree.isolutions.property.PropertiesConfig.getApplicationSettings();
            if(appCfg.hasProperty("JDBCDriverName") == false){				throw new IllegalArgumentException("JDBC Driver not specified!");			}else{				jdbcDriver = appCfg.getProperty("JDBCDriverName");			}
            if(appCfg.hasProperty("JDBCDataSourceName") == false){				throw new IllegalArgumentException("JDBC Data Source not specified!");			}else{				jdbcDataSource = appCfg.getProperty("JDBCDataSourceName");			}
			if(appCfg.hasProperty("DataBaseUserid") == false){                throw new IllegalArgumentException("Database userid not specified!");			}else{                username = appCfg.getProperty("DataBaseUserid");			}
            if(appCfg.hasProperty("DataBasePassword") == false){                throw new IllegalArgumentException("Database password not specified!");            }else{                password = appCfg.getProperty("DataBasePassword");			}
			Class.forName(jdbcDriver);			conn = DriverManager.getConnection(jdbcDataSource, username, password);			System.out.println("--- Connection opened ---");		}catch(Exception e) {			throw new Exception("Error getting DB connection: " + e);		}
        return conn;    }// end getDbConnection

    public static void freeDbConnection(Connection conn) throws Exception {        try {			if (conn != null){				conn.close();				System.out.println("--- Connection closed ---");			}		}catch(Exception e) {            throw new Exception("Error closing DB connection : " + e);        }   }// end freeDbConnection


//////////////////////////////////////////////////////////////////////////////////////    public static EbppxCorpCustomer getCorpCustomer(Connection connection,String strUserName)throws Exception {
        PreparedStatement pstmt = null;
        ResultSet rs= null;
        StringBuffer query = new StringBuffer();
        EbppxCorpCustomer corpCust = null;

        try{
			if (connection!=null){

				//search for status records first
				query.append("SELECT TRIM(USERID),TRIM(PASSWORD),TRIM(EMAILADD),TRIM(ACCOUNTS) FROM CORPLOGIN WHERE USERID = ?");

				// prepare the statement and bind parameter
				pstmt = connection.prepareStatement(query.toString());
				pstmt.setString(1,strUserName);

				rs   = pstmt.executeQuery();

				if(rs.next()){
					System.out.println("In corp login get details.....");
					corpCust = new EbppxCorpCustomer();
					corpCust.setUserName(rs.getString(1));//user name
					corpCust.setPassword(rs.getString(2));//password
					corpCust.setEmailAdd(rs.getString(3));//email address
					corpCust.setAccounts(rs.getString(4));//accounts
					System.out.println("End corp login get details.....");
				}

				if (pstmt != null) {
					pstmt.close();
				}
			}

			System.out.println("Retrieve corp login details has been successfully executed");

		}catch(Exception e){
			throw new Exception("" + e);
		}finally{
			if(rs!=null){
				rs=null;
			}
		}
		return corpCust;
	}


    public static EbppxCorpCustomer getCorpCustomer(String strUserName)throws Exception {
		Connection connection = null;
        PreparedStatement pstmt = null;
        ResultSet rs= null;
        StringBuffer query = new StringBuffer();
        EbppxCorpCustomer corpCust = null;

        try{
			connection = getDbConnection();

			if (connection!=null){
				corpCust = new EbppxCorpCustomer();
				corpCust = getCorpCustomer(connection,strUserName);
			}

			System.out.println("Retrieve corp login details has been successfully executed");

		}catch(Exception e){
            throw new Exception("" + e);
        }finally{
            if(rs!=null){
                rs=null;
            }
            if(connection!=null){
                freeDbConnection(connection);
            }
        }
        return corpCust;
    }

	public void setEmailAdd(String strEmailAdd) {
		if(strEmailAdd==null || strEmailAdd.equals("")){
			this.emailAdd = "";
		}else{
			this.emailAdd = strEmailAdd;
		}
	}

	public String getEmailAdd() {
		return this.emailAdd;
	}

	public void setUserName(String strUserName) {
		if(strUserName==null || strUserName.equals("")){
			this.userName = "";
		}else{
			this.userName = strUserName;
		}
	}

	public String getUserName() {
		return this.userName;
	}

	public void setPassword(String strPassword) {
		if(strPassword==null || strPassword.equals("")){
			this.password = "";
		}else{
			this.password = strPassword;
		}
	}

	public String getPassword() {
		return this.password;
	}

	public void setAccounts(String strAccounts) {
		if(strAccounts==null || strAccounts.equals("")){
			this.accounts = "";
		}else{
			this.accounts = strAccounts;
		}
	}

	public String getAccounts() {
		return this.accounts;
	}
}








